package org.ilovecode.animation {
	
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.TimerEvent;
	import flash.geom.Rectangle;
	import flash.utils.Timer;
	import flash.utils.getDefinitionByName;
	import flash.utils.getQualifiedClassName;
	
	import gs.TweenLite;
	
	import org.ilovecode.mvc.View;
	
	/**
	 * ...
	 * @author Vjekoslav Ratkajec
	 */
	public class Animation extends View {
		
		private var timer:Timer;
		private var _drawingArea:Rectangle;
		private var count:uint;
		private var baseObject:MovieClip;
		private var _animationName:String = "";
		
		private const OFFSET:Number = 100;
		private var maxRadius:Number = 0;
		
		public function Animation(baseObject:MovieClip, animationName:String, count:uint, drawingArea:Rectangle) {
			super();
			
			this.baseObject = baseObject;
			this._animationName = animationName;
			this.count = count;
			this._drawingArea = drawingArea;
		}
		
		override protected function construct():void
		{
			super.construct();
			
			timer = new Timer(500, count);
		}
		
		override protected function initUI():void
		{
			super.initUI();
			
			if (_drawingArea.width < OFFSET || _drawingArea.height < OFFSET) {
				throw new Error("Error in intro drawing size");
			}
			
			if (_drawingArea.width > _drawingArea.height) {
				maxRadius = _drawingArea.height;
			} else {
				maxRadius = _drawingArea.width;
			}
			
			timer.start();
		}
		
		
		override protected function addListeners():void
		{
			super.addListeners();
			timer.addEventListener(TimerEvent.TIMER, update);
			timer.addEventListener(TimerEvent.TIMER_COMPLETE, update);
		}
		
		override protected function removeListeners():void
		{
			super.removeListeners();
			timer.removeEventListener(TimerEvent.TIMER, update);
			timer.removeEventListener(TimerEvent.TIMER_COMPLETE, update);
		}
		
		override protected function update(event:Event):void
		{
			switch(event.type)
			{
				case TimerEvent.TIMER:
					render();
					break;
				case TimerEvent.TIMER_COMPLETE:
					this.dispatchEvent(new Event(Event.COMPLETE));
					break;
				default:
					super.update(event);
					break;
			}
		}
		
		private function render():void {
			var randomBaseRadius:Number = Math.random() * OFFSET;
			var radius:Number = randomBaseRadius + maxRadius - OFFSET;
			var randomRotation:Number;
			if (animationName == "blue") {
				randomRotation = 180 + Math.random() * 180;
			} else if (animationName == "green") {
				randomRotation = Math.random() * 180;
			} else {
				throw new Error("Unknown color");
			}
			
			var baseObjectClass:Class = Class(getDefinitionByName(getQualifiedClassName(baseObject)));
			var newBaseObject:MovieClip = new baseObjectClass();
			newBaseObject.width = newBaseObject.height = radius;
			newBaseObject.x = _drawingArea.x - Math.random() * (randomBaseRadius) / 2;
			newBaseObject.y = _drawingArea.y - Math.random() * (randomBaseRadius) / 2;
			newBaseObject.rotation = randomRotation;
			
			this.addChild(newBaseObject);
		}
		
		public function get animationName():String {
			return _animationName;
		}
		
		public function get drawingArea():Rectangle {
			return _drawingArea;
		}
		
	}

}